package com.itheima.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer_PubSub2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2.设置参数
        factory.setHost("172.81.237.219");//ip地址
        factory.setPort(5672);
        factory.setVirtualHost("/itcast");//虚拟机
        factory.setUsername("heima");
        factory.setPassword("heima");
        //3.创建连接
        Connection connection = factory.newConnection();
        //4.创建channel
        Channel channel = connection.createChannel();

        //6.接收消息
        /*String queue,队列名称
        boolean autoAck,是否自动确认
        Consumer callback。回调函数
        * */
        String queue1Name="test_fanout_queue1";
        String queue2Name="test_fanout_queue2";

        Consumer consumer = new DefaultConsumer(channel){
            //回调方法。
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                System.out.println("consumerTag:"+consumerTag);
//                System.out.println("Exchange:"+envelope.getExchange());
//                System.out.println("RoutingKey:"+envelope.getRoutingKey());
//                System.out.println("properties:"+properties);
                System.out.println("body:"+new String(body));
                System.out.println("把日志信息保存数据库");
            }
        };

        channel.basicConsume(queue2Name,true,consumer);
    }
}
